package com.tontron.web.controller.system;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tontron.system.service.SysTenantService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tontron.common.annotation.Log;
import com.tontron.common.core.controller.BaseController;
import com.tontron.common.core.domain.AjaxResult;
import com.tontron.common.enums.BusinessType;
import com.tontron.system.domain.SysTenant;
import com.tontron.common.utils.poi.ExcelUtil;
import com.tontron.common.core.page.TableDataInfo;

/**
 * 租户Controller
 *
 * @author ruoyi
 * @date 2024-07-05
 */
@RestController
@RequestMapping("/system/tenant")
public class SysTenantController extends BaseController
{
    @Autowired
    private SysTenantService sysTenantService;

    /**
     * 查询租户列表
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:list')")
    @GetMapping("/list")
    public TableDataInfo list(Page page, SysTenant sysTenant){
        IPage<SysTenant> data = sysTenantService.listByPage(page,sysTenant);
        return getDataTable(data);
    }


    /**
     *  返回所有租户列表不带分页
     * @param page
     * @param sysTenant
     * @return
     */
    @GetMapping("/listAll")
    public AjaxResult listAll(SysTenant sysTenant){
        return success(sysTenantService.listAll(sysTenant));
    }

    /**
     * 导出租户列表
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:export')")
    @Log(title = "导出租户列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysTenant sysTenant)
    {
//        List<SysTenant> list = sysTenantService.selectSysTenantList(sysTenant);
//        ExcelUtil<SysTenant> util = new ExcelUtil<SysTenant>(SysTenant.class);
//        util.exportExcel(response, list, "租户数据");
    }

    /**
     * 获取租户详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:query')")
    @GetMapping(value = "/{tenantId}")
    public AjaxResult getInfo(@PathVariable("tenantId") Long tenantId)
    {
        return success(sysTenantService.getOptById(tenantId));
    }

    /**
     * 新增租户
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:add')")
    @Log(title = "新增租户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysTenant sysTenant)
    {
        //新增租户都是普通租户
        sysTenant.setTenantType(SysTenant.TENANT_TYPE_CUSTOMER);
        return toAjax(sysTenantService.save(sysTenant));
    }

    /**
     * 修改租户
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:edit')")
    @Log(title = "修改租户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysTenant sysTenant)
    {

        if(sysTenant.getTenantId()==1){
            return error("不允许修改系统默认租户信息");
        }

        return toAjax(sysTenantService.updateById(sysTenant));
    }


    @PreAuthorize("@ss.hasPermi('system:tenant:edit')")
    @Log(title = "修改租户状态", businessType = BusinessType.UPDATE)
    @PutMapping("/status/update/{tenantId}/{status}")
    public AjaxResult editStatus(@PathVariable("tenantId") Long tenantId,@PathVariable("status") String status)
    {


        SysTenant sysTenant = sysTenantService.getById(tenantId);

        if(sysTenant.getTenantId()==1){
            return error("不允许修改系统默认租户信息");
        }

        SysTenant param = new SysTenant();
        param.setTenantId(tenantId);
        param.setStatus(status);

        return toAjax(sysTenantService.updateById(param));
    }

    /**
     * 删除租户
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:remove')")
    @Log(title = "删除租户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tenantIds}")
    public AjaxResult remove(@PathVariable Long[] tenantIds)
    {
        if(Arrays.stream(tenantIds).anyMatch(id->id==1)){
            return error("不允许修改系统默认租户信息");
        }
        return toAjax(sysTenantService.removeByIds(CollectionUtils.arrayToList(tenantIds)));
    }
}
